package com.hs.service;

import com.hs.VO.LessonPO;
import com.hs.VO.LessonStudentVO;
import com.hs.config.PageObj;
import com.hs.dao.ClassLessonIssueMapper;
import com.hs.dao.ClassLessonMapper;
import com.hs.dao.IssueFlushMapper;
import com.hs.dao.LessonMapper;
import com.hs.entity.ClassLesson;
import com.hs.entity.ClassLessonIssue;
import com.hs.entity.IssueFlush;
import com.hs.entity.Lesson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class LessonService {
    @Autowired
    LessonMapper lessonMapper;
    @Autowired
    ClassLessonIssueMapper classLessonIssueMapper;
    @Autowired
    ClassLessonMapper classLessonMapper;
    @Autowired
    IssueFlushMapper issueFlushMapper;
    @Transactional
    public int insertSelective(Lesson record){
        if(record.getImgUrl() == null){
            record.setImgUrl("/img/default.png");
        }
        record.setCreateTime(new Date());
        int i = lessonMapper.insertSelective(record);
        if(i == 0){
            throw new RuntimeException("用户插入失败");
        }
        return  i;
    };
    public int updateByPrimaryKeySelective(Lesson record){
        return lessonMapper.updateByPrimaryKeySelective(record);
    };
    public List<Lesson> selectByCondition(Lesson record){
        return lessonMapper.selectByCondition(record);
    }
    public Lesson selectByPrimaryKey(Integer id){
        return lessonMapper.selectByPrimaryKey(id);
    }
    public Lesson selectInfoByPrimaryKey(Integer id){
        return lessonMapper.selectInfoByPrimaryKey(id);
    }
    //查询课程信息  以 classLesson 为粒度  ，附带 model 信息，方便插入classLessonIssue。
    //附带 classLessonIssue信息
    @Transactional
    public LessonStudentVO selectStudentLessonInfo(LessonPO lessonPO){
        //查询课程 以classLesson为粒度
        List<LessonStudentVO> list = lessonMapper.selectStudentLesson(lessonPO);
        if(list == null || list.size() == 0){
            log.error("classLesson 数据不存在！");
            return null;
        }
        LessonStudentVO item = list.get(0);

//   ----------------------附带 classLessonIssue信息开始，没有则创建，有则返回最新-----------------------

        int issues = 0;//获取具体最新期数
        Date createTime = null;//获取最新的期数的创建时间
        Integer classLessonIssueId = 0;
        //课程还未开始，返回默认数据。现构建LessonStudentVO默认属性
        if(item.getStatus() == 0){
            item.setIssues(0);
            item.setCreateTime(createTime);
            item.setClassLessonIssueId(classLessonIssueId);
            return item;
        }

        //如已开始或则进行中
        ClassLessonIssue param = new ClassLessonIssue();
        param.setClassLessonId(item.getId());
        //默认 order by createTime desc 但是 IssuesFLush默认是order by createTime  asc 方便line展示
        List<ClassLessonIssue> classLessonIssues = classLessonIssueMapper.selectByCondition(param);
        //没有数据 开始初始化第一条 期数数据  ClassLessonIssue
        if(null == classLessonIssues || classLessonIssues.size() == 0){
            param.setCreateTime(new Date());
            param.setNeedA(item.getNeedA());
            param.setNeedB(item.getNeedB());
            param.setNeedTrid(item.getNeedtrid());
            param.setProvideA(item.getProvideA());
            param.setProvideB(item.getProvideB());
            param.setProvideTrid(item.getProvideTrid());
            param.setIssue(1);
            issues = 1;
            createTime = new Date();
            int i = classLessonIssueMapper.insertSelective(param);
            classLessonIssueId = param.getId();
            if(i > 0 ){
                log.info("课程'"+item.getName()+"' 流水插入第一期，第一期开始。");
            }else{
                log.error("课程'"+item.getName()+"' 流水插入第一期，第一期开始失败。");
            }
        }else{
            //如果不为空，已有数据，并为已结束，则初始化后续以次类推  期数。
            ClassLessonIssue classLessonIssue = classLessonIssues.get(0);
            if(classLessonIssue.getStatus() == 1  && classLessonIssue.getIssue() < item.getIssue()){
                //计算偏加减移量
                IssueFlush issueFlush = new IssueFlush();
                issueFlush.setIssueId(classLessonIssue.getId());
                issueFlush.setOrderDesc(" order by create_time desc ");
                List<IssueFlush> issueFlushes = issueFlushMapper.selectByCondition(issueFlush);
                if(issueFlushes!=null && issueFlushes.size() > 0){
                    //计算偏移量   ==    需求大于供给 +30单位
                    int flag = issueFlushes.get(0).getBPP().intValue();
                    if(flag > 0 ){
                        param.setProvideB(item.getProvideB() + item.getMoveLimit());
                    }else {
                        param.setProvideB(item.getProvideB() - item.getMoveLimit());

                    }
                }

                param.setCreateTime(new Date());
                param.setNeedA(item.getNeedA());
                param.setNeedB(item.getNeedB());
                param.setNeedTrid(item.getNeedtrid());

                param.setProvideA(item.getProvideA());
                param.setProvideTrid(item.getProvideTrid());

                param.setIssue(classLessonIssue.getIssue() + 1);
                int i = classLessonIssueMapper.insertSelective(param);
                classLessonIssueId = param.getId();

                if(i > 0 ){
                    issues = classLessonIssue.getIssue() + 1;
                    log.info("课程'"+item.getName()+"' 流水插入第"+issues+"期，第"+issues+"期开始。");
                    createTime = new Date();
                }

            }else{
                // 如果进行中，则直接返回 查询到的期数 数据。
                issues = classLessonIssue.getIssue();
                createTime = classLessonIssue.getCreateTime();
                classLessonIssueId = classLessonIssue.getId();
            }
        }

        item.setIssues(issues);
        item.setCreateTime(createTime);
        item.setClassLessonIssueId(classLessonIssueId);
        //   ----------------------附带 classLessonIssue信息结束-----------------------
        return item;
    }

    public PageObj page(Lesson item){
        int total = lessonMapper.selectByConditionCount(item);
        if(total == 0){
            return new PageObj(item.getCurrentPage(), item.getPageSize(),total,null);
        }
        List<Lesson> list = lessonMapper.selectByCondition(item);
        return new PageObj(item.getCurrentPage(), item.getPageSize(),total,list);
    }
    public PageObj studentLessonPage(LessonPO item){


        int total = lessonMapper.selectStudentLessonCount(item);
        if(total == 0){
            return new PageObj(item.getCurrentPage(), item.getPageSize(),total,null);
        }
        List<LessonStudentVO> list = lessonMapper.selectStudentLesson(item);
        return new PageObj(item.getCurrentPage(), item.getPageSize(),total,list);
    }


    public PageObj teacherLessonPage(LessonPO item){
        int total = lessonMapper.selectTeacherLessonCount(item);
        if(total == 0){
            return new PageObj(item.getCurrentPage(), item.getPageSize(),total,null);
        }
        List<LessonStudentVO> list = lessonMapper.selectTeacherLesson(item);
        return new PageObj(item.getCurrentPage(), item.getPageSize(),total,list);
    }
    public List<LessonStudentVO> selectTeacherLesson(LessonPO item){
        List<LessonStudentVO> list = lessonMapper.selectTeacherLesson(item);
        return list;
    }
    public int selectByConditionCount(Lesson record){
        int count =  lessonMapper.selectByConditionCount(record);
        return count;
    }


}
